package com.example.game1.schedule2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.List;

import database.User;

public class Main1Activity extends AppCompatActivity {

    private Button bt_resigter;

    private EditText account_edit;
    private EditText password_edit;
    private EditText password_edit2;
    private String account;
    private String password;
    private String password2;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        Connector.getDatabase();
        account_edit = (EditText)findViewById(R.id.account_edit);
        password_edit = (EditText)findViewById(R.id.password_edit);
        password_edit2 = (EditText)findViewById(R.id.password_edit2);
        bt_resigter = (Button)findViewById(R.id.bt_register);
        regisiter();
    }



    //注册
    public void regisiter(){
        bt_resigter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                account = account_edit.getText().toString().trim();
                password = password_edit.getText().toString().trim();
                password2 = password_edit2.getText().toString().trim();
                //判断帐号密码是否为空
                if (account.equals("") || password.equals("")|| password2.equals("")) {
                    Toast.makeText(Main1Activity.this, "帐号或密码不能为空", Toast.LENGTH_SHORT).show();
                } else{

                    // 判断两次输入的密码是否相同
                    if (!password.equals(password2)) {
                        Toast.makeText(Main1Activity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                        return; // 如果密码不一致，则直接返回，不再继续执行
                    }
                    //判断帐号是否已存在
                    List<User> users = DataSupport.findAll(User.class);
                    Boolean flag = false;
                    for (User u:users) {
                        if(u.getAccount().equals(account) ){
                            flag = true;
                            Toast.makeText(Main1Activity.this,"错误，该帐号已存在！！！",Toast.LENGTH_SHORT).show();
                            break;
                        }
                    }
                    //若帐号密码不为空且不存在，则将帐号信息添加到数据库中
                    if(!flag){
                        User user = new User();
                        user.setAccount(account);
                        user.setPassword(password);
                        user.save();
                        Toast.makeText(Main1Activity.this,"注册成功！！！",Toast.LENGTH_SHORT).show();

                        // 跳转到 MainActivity
                        Intent intent = new Intent(Main1Activity.this, MainActivity.class);
                        startActivity(intent);

                        // 关闭当前活动（注册页面）
                        finish();
                    }
                }
            }
        });
    }
}
